package com.teziyou.commodity.logic;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teziyou.commodity.enumtype.EnableEnum;
import com.teziyou.commodity.service.LineService;
import com.teziyou.commodity.service.SceneService;
import com.teziyou.commodity.service.TicketService;
import com.teziyou.commodity.util.Lang;
import com.teziyou.commodity.util.Lists;

@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class LineLogic extends LineService {
	
	@Autowired
	private TicketService ticketService;
	@Autowired
	private SceneService sceneService;
	
	public List listLines(Map<String, String> paramMap) {
		List lineList = super.findLinesByPage(paramMap);
		Iterator iter = lineList.iterator();
		while(iter.hasNext()) {
			Map line = (Map)iter.next();
			long lineId = Lang.toLong(line.get("travel_product_id"));
			List scenes = null;//super.findRelationByLineId(lineId);
			Iterator sIter = scenes.iterator();
			String sceneName = "";
			while(sIter.hasNext()) {
				Map relation = (Map)sIter.next();
				long sceneId = Lang.toLong(relation.get("scenic_id"));
				Map scene = sceneService.findSceneById(sceneId);
				sceneName += scene.get("name");
				if(sIter.hasNext())  sceneName += ",";
			}
			
			line.put("scene_name", sceneName);
			line.put("isOK_display", EnableEnum.getEnumByCode(Lang.toInt(line.get("isOK"))).getDesc());
		}
		return lineList;
	}
	
	public List loadLines(String ids) {
		List list = Lists.newArrayList();
		if(StringUtils.isNotBlank(ids)) {
			String[] idStrs = ids.split(",");
			for(String id:idStrs) {
				if(StringUtils.isNotBlank(id)) {
					Map line = null;//super.findLineById(Long.parseLong(id));
					long lineId = Lang.toLong(line.get("travel_product_id"));
					List scenes = null;//super.findRelationByLineId(lineId);
					Iterator sIter = scenes.iterator();
					String sceneName = "";
					while(sIter.hasNext()) {
						Map relation = (Map)sIter.next();
						long sceneId = Lang.toLong(relation.get("scenic_id"));
						Map scene = sceneService.findSceneById(sceneId);
						sceneName += scene.get("name");
						if(sIter.hasNext())  sceneName += ",";
					}
					
					line.put("scene_name", sceneName);
					line.put("isOK_display", EnableEnum.getEnumByCode(Lang.toInt(line.get("isOK"))).getDesc());
					list.add(line);
				}
			}
		}
		return list;
	}

}
